package com.fh.util;

import java.security.MessageDigest;

public class MD5 {

	public static String md5(String str) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(str.getBytes());
			byte b[] = md.digest();

			int i;

			StringBuffer buf = new StringBuffer("");
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0)
					i += 256;
				if (i < 16)
					buf.append("0");
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
		} catch (Exception e) {
			e.printStackTrace();

		}
		return str;
	}
	public static void main(String[] args) {
		System.out.println(md5("31119@qq.com"+"123456"));
		System.out.println(md5("mj1"));
	}
	
	/**
	  * 验证字符串是否匹配
	  * @param unknown 待验证的字符串
	  * @param okHex 使用MD5加密过的16进制字符串
	  * @return 匹配返回true，不匹配返回false
	  */
//	 public static boolean validate(String unknown , String okHex) {
//	  return okHex.equals(md5(unknown));
//	 }

}
